﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogManager.Base;
using LogManager.Base.Models.Projections;
using Raven.Client.Indexes;

namespace LogManager.LocalStorage.Indexes
{
    public class Events_LevelsPerLog : AbstractIndexCreationTask<Event, LevelsPerLogProjection>
    {
        public Events_LevelsPerLog()
        {
            Map = events =>
                  from e in events
                  select new
                             {
                                 LogName = e.LogName,
                                 Critical = e.Level == "Critical" ? 1 : 0,
                                 Warning = e.Level == "Warning" ? 1 : 0,
                                 Error = e.Level == "Error" ? 1 : 0,
                                 Information = e.Level == "Information" ? 1 : 0
                             };
            Reduce = levels =>
                     from l in levels
                     group l by l.LogName
                     into g
                     select new
                                {
                                    LogName = g.Key,
                                    Critical = g.Sum(x => x.Critical),
                                    Warning = g.Sum(x => x.Warning),
                                    Error = g.Sum(x => x.Error),
                                    Information = g.Sum(x => x.Information)
                                };
        }
    }
}
